package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import control.*;
import model.*;

import java.awt.Font;

import javax.swing.SwingConstants;
import javax.swing.JTable;
import javax.swing.JScrollPane;

public class ProductMenu extends JFrame {

	private JPanel contentPane;
	private static MainMenu mm = new MainMenu();
	private ProductCtrl proCtrl = new ProductCtrl();
	private SupplierCtrl supCtrl = new SupplierCtrl();
	//Supplier
	private JTextField txtSname;
	private JTextField txtSaddress;
	private JTextField txtScountry;
	private JTextField txtSphoneNo;
	private JTextField txtSemail;
	//Clothing
	private JTextField txtCid;
	private JTextField txtCname;
	private JTextField txtCpurchasePrice;
	private JTextField txtCsalesPrice;
	private JTextField txtCrentPrice;
	private JTextField txtCcountry;
	private JTextField txtCminStock;
	private JTextField txtCsupplierId;
	private JTextField txtCpType;
	private JTextField txtCsize;
	private JTextField txtCcolor;
	private JLabel lblCid;
	private JLabel lblCname;
	private JLabel lblCpurchasePrice;
	private JLabel lblCsalesPrice;
	private JLabel lblCrentPrice;
	private JLabel lblCcountry;
	private JLabel lblCminStock;
	private JLabel lblCsupplierId;
	private JLabel lblCpType;
	private JLabel lblCsize;
	private JLabel lblCcolor;
	private JButton btnCcreate;
	private JButton btnCfind;
	private JButton btnCdelete;
	private JButton btnCupdate;
	//Equipment
	private JTextField txtEid;
	private JTextField txtEname;
	private JTextField txtEpurchasePrice;
	private JTextField txtEsalesPrice;
	private JTextField txtErentPrice;
	private JTextField txtEcountry;
	private JTextField txtEminStock;
	private JTextField txtEsupplierId;
	private JTextField txtEpType;
	private JTextField txtEtype;
	private JTextField txtEdescription;
	private JLabel lblEmain;
	private JLabel lblEname;
	private JLabel lblEpurchasePrice;
	private JLabel lblEsalesPrice;
	private JLabel lblErentPrice;
	private JLabel lblEcountry;
	private JLabel lblEminStock;
	private JLabel lblEsupplierId;
	private JLabel lblEproductType;
	private JLabel lblEtype;
	private JLabel lblEdescription;
	private JButton btnEcreate;
	private JButton btnEfind;
	private JButton btnEdelete;
	private JButton btnEupdate;
	//Gun Replicas
	private JTextField txtGid;
	private JTextField txtGname;
	private JTextField txtGpurchasePrice;
	private JTextField txtGsalesPrice;
	private JTextField txtGrentPrice;
	private JTextField txtGcountry;
	private JTextField txtGminStock;
	private JTextField txtGsupplierId;
	private JTextField txtGpType;
	private JTextField txtGfabric;
	private JTextField txtGcaliber;
	private JLabel lblGmain;
	private JLabel lblGid;
	private JLabel lblGname;
	private JLabel lblGpurchasePrice;
	private JLabel lblGsalesPrice;
	private JLabel lblGrentPrice;
	private JLabel lblGcountry;
	private JLabel lblGminStock;
	private JLabel lblGsupplierId;
	private JLabel lblGpType;
	private JLabel lblGfabric;
	private JLabel lblGcaliber;
	private JButton btnGcreate;
	private JButton btnGfind;
	private JButton btnGdelete;
	private JButton btnGupdate;
	//Tables and stuff
	private static SupplierTableList sList;
	private ProductTable pTable = new ProductTable();
	private JTable table;
	private JButton btnSclear;
	private JButton btnCclear;
	private JButton btnEclear;
	private JButton btnGclear;
	private JScrollPane scrollPane;
	private JButton btnListProducts;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductMenu frame = new ProductMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProductMenu() {
		setTitle("Products");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 640, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 624, 312);
		contentPane.add(tabbedPane);
		
		JPanel supPanel = new JPanel();
		tabbedPane.addTab("Supplier", null, supPanel, null);
		supPanel.setLayout(null);
		
		JPanel cloPanel = new JPanel();
		tabbedPane.addTab("Clothing", null, cloPanel, null);
		cloPanel.setLayout(null);
		
		JLabel lblCmain = new JLabel("Create - Find - Delete - Update");
		lblCmain.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCmain.setBounds(211, 11, 193, 14);
		cloPanel.add(lblCmain);
		
		txtCid = new JTextField();
		txtCid.setColumns(10);
		txtCid.setBounds(101, 36, 110, 20);
		cloPanel.add(txtCid);
		
		txtCname = new JTextField();
		txtCname.setColumns(10);
		txtCname.setBounds(101, 67, 110, 20);
		cloPanel.add(txtCname);
		
		txtCpurchasePrice = new JTextField();
		txtCpurchasePrice.setColumns(10);
		txtCpurchasePrice.setBounds(101, 98, 110, 20);
		cloPanel.add(txtCpurchasePrice);
		
		txtCsalesPrice = new JTextField();
		txtCsalesPrice.setColumns(10);
		txtCsalesPrice.setBounds(101, 129, 110, 20);
		cloPanel.add(txtCsalesPrice);
		
		txtCrentPrice = new JTextField();
		txtCrentPrice.setColumns(10);
		txtCrentPrice.setBounds(101, 160, 110, 20);
		cloPanel.add(txtCrentPrice);
		
		txtCcountry = new JTextField();
		txtCcountry.setColumns(10);
		txtCcountry.setBounds(101, 191, 110, 20);
		cloPanel.add(txtCcountry);
		
		txtCminStock = new JTextField();
		txtCminStock.setColumns(10);
		txtCminStock.setBounds(400, 36, 110, 20);
		cloPanel.add(txtCminStock);
		
		txtCsupplierId = new JTextField();
		txtCsupplierId.setColumns(10);
		txtCsupplierId.setBounds(400, 67, 110, 20);
		cloPanel.add(txtCsupplierId);
		
		txtCpType = new JTextField();
		txtCpType.setColumns(10);
		txtCpType.setBounds(400, 98, 110, 20);
		cloPanel.add(txtCpType);
		
		txtCsize = new JTextField();
		txtCsize.setColumns(10);
		txtCsize.setBounds(400, 129, 110, 20);
		cloPanel.add(txtCsize);
		
		txtCcolor = new JTextField();
		txtCcolor.setColumns(10);
		txtCcolor.setBounds(400, 160, 110, 20);
		cloPanel.add(txtCcolor);
		
		lblCid = new JLabel("ID : ");
		lblCid.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCid.setBounds(-26, 39, 117, 14);
		cloPanel.add(lblCid);
		
		lblCname = new JLabel("Name : ");
		lblCname.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCname.setBounds(-26, 70, 117, 14);
		cloPanel.add(lblCname);
		
		lblCpurchasePrice = new JLabel("Purchase Price : ");
		lblCpurchasePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCpurchasePrice.setBounds(-26, 101, 117, 14);
		cloPanel.add(lblCpurchasePrice);
		
		lblCsalesPrice = new JLabel("Sales Price : ");
		lblCsalesPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCsalesPrice.setBounds(-26, 132, 117, 14);
		cloPanel.add(lblCsalesPrice);
		
		lblCrentPrice = new JLabel("Rent Price : ");
		lblCrentPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCrentPrice.setBounds(-26, 163, 117, 14);
		cloPanel.add(lblCrentPrice);
		
		lblCcountry = new JLabel("Country Origin : ");
		lblCcountry.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCcountry.setBounds(-26, 194, 117, 14);
		cloPanel.add(lblCcountry);
		
		lblCminStock = new JLabel("Minimum Stock : ");
		lblCminStock.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCminStock.setBounds(273, 39, 117, 14);
		cloPanel.add(lblCminStock);
		
		lblCsupplierId = new JLabel("Supplier ID : ");
		lblCsupplierId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCsupplierId.setBounds(273, 70, 117, 14);
		cloPanel.add(lblCsupplierId);
		
		lblCpType = new JLabel("Product Type : ");
		lblCpType.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCpType.setBounds(273, 101, 117, 14);
		cloPanel.add(lblCpType);
		
		lblCsize = new JLabel("Size : ");
		lblCsize.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCsize.setBounds(273, 132, 117, 14);
		cloPanel.add(lblCsize);
		
		lblCcolor = new JLabel("Color : ");
		lblCcolor.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCcolor.setBounds(273, 163, 117, 14);
		cloPanel.add(lblCcolor);
		
		btnCcreate = new JButton("Create");
		btnCcreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
				proCtrl.insertClothing(Integer.parseInt(txtCid.getText()), txtCname.getText(), Double.parseDouble(txtCpurchasePrice.getText()), Double.parseDouble(txtCsalesPrice.getText()),
						Double.parseDouble(txtCrentPrice.getText()), txtCcountry.getText(), Integer.parseInt(txtCminStock.getText()), Integer.parseInt(txtCsupplierId.getText()),
						txtCpType.getText(), txtCsize.getText(), txtCcolor.getText());
				JOptionPane.showMessageDialog(null,"Clothing product was created!");
				clearClothingTextFields();
				pTable.updateProductTable();
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Clothing!");
				}
			}
		});
		btnCcreate.setBounds(10, 222, 110, 23);
		cloPanel.add(btnCcreate);
		
		btnCfind = new JButton("Find");
		btnCfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = proCtrl.getProductID(txtCid.getText());
				txtCname.setText(prod.getName());
				txtCpurchasePrice.setText(String.valueOf(prod.getPurchasePrice()));
				txtCsalesPrice.setText(String.valueOf(prod.getSalesPrice()));
				txtCrentPrice.setText(String.valueOf(prod.getRentPrice()));
				txtCcountry.setText(prod.getCountry());
				txtCminStock.setText(String.valueOf(prod.getMinStock()));
				txtCsupplierId.setText(String.valueOf(prod.getSupplierID()));
				txtCpType.setText(prod.getpType());
				Clothing clot = proCtrl.getClothingID(txtCid.getText());
				txtCsize.setText(clot.getSize());
				txtCcolor.setText(clot.getColor());
			}
		});
		btnCfind.setBounds(130, 222, 110, 23);
		cloPanel.add(btnCfind);
		
		btnCdelete = new JButton("Delete");
		btnCdelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				proCtrl.deleteProduct(txtCid.getText());
				proCtrl.deleteClothing(txtCid.getText());
				clearClothingTextFields();
				pTable.updateProductTable();
			}
		});
		btnCdelete.setBounds(250, 222, 110, 23);
		cloPanel.add(btnCdelete);
		
		btnCupdate = new JButton("Update");
		btnCupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = new Product();
				prod = proCtrl.getProductID(txtCid.getText());
				if(!txtCname.getText().equals("")) prod.setName(txtCname.getText());
				if(!txtCpurchasePrice.getText().equals("")) prod.setPurchasePrice(Double.parseDouble(txtCpurchasePrice.getText()));
				if(!txtCsalesPrice.getText().equals("")) prod.setSalesPrice(Double.parseDouble(txtCsalesPrice.getText()));
				if(!txtCrentPrice.getText().equals("")) prod.setRentPrice(Double.parseDouble(txtCrentPrice.getText()));
				if(!txtCcountry.getText().equals("")) prod.setCountry(txtCcountry.getText());
				if(!txtCminStock.getText().equals("")) prod.setMinStock(Integer.parseInt(txtCminStock.getText()));
				if(!txtCsupplierId.getText().equals("")) prod.setSupplierID(Integer.parseInt(txtCsupplierId.getText()));
				if(!txtCpType.getText().equals("")) prod.setpType(txtCpType.getText());
				Clothing clot = new Clothing();
				clot = proCtrl.getClothingID(txtCid.getText());
				if(!txtCsize.getText().equals("")) clot.setSize(txtCsize.getText());
				if(!txtCcolor.getText().equals("")) clot.setColor(txtCcolor.getText());
				proCtrl.updateClothing(prod.getId(), prod.getName(), prod.getPurchasePrice(), prod.getSalesPrice(), prod.getRentPrice(), prod.getCountry(), prod.getMinStock(), prod.getSupplierID(), prod.getpType(), clot.getSize(), clot.getColor());
				pTable.updateProductTable();
			}
		});
		btnCupdate.setBounds(370, 222, 110, 23);
		cloPanel.add(btnCupdate);
		
		JButton btnCquit = new JButton("Quit");
		btnCquit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnCquit.setBounds(519, 248, 90, 25);
		cloPanel.add(btnCquit);
		
		btnCclear = new JButton("Clear Fields");
		btnCclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearClothingTextFields();
			}
		});
		btnCclear.setBounds(509, 190, 100, 25);
		cloPanel.add(btnCclear);
		
		JPanel equPanel = new JPanel();
		tabbedPane.addTab("Equipment", null, equPanel, null);
		equPanel.setLayout(null);
		
		lblEmain = new JLabel("Create - Find - Delete - Update");
		lblEmain.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblEmain.setBounds(211, 11, 193, 14);
		equPanel.add(lblEmain);
		
		txtEid = new JTextField();
		txtEid.setColumns(10);
		txtEid.setBounds(101, 36, 110, 20);
		equPanel.add(txtEid);
		
		txtEname = new JTextField();
		txtEname.setColumns(10);
		txtEname.setBounds(101, 67, 110, 20);
		equPanel.add(txtEname);
		
		txtEpurchasePrice = new JTextField();
		txtEpurchasePrice.setColumns(10);
		txtEpurchasePrice.setBounds(101, 98, 110, 20);
		equPanel.add(txtEpurchasePrice);
		
		txtEsalesPrice = new JTextField();
		txtEsalesPrice.setColumns(10);
		txtEsalesPrice.setBounds(101, 129, 110, 20);
		equPanel.add(txtEsalesPrice);
		
		txtErentPrice = new JTextField();
		txtErentPrice.setColumns(10);
		txtErentPrice.setBounds(101, 160, 110, 20);
		equPanel.add(txtErentPrice);
		
		txtEcountry = new JTextField();
		txtEcountry.setColumns(10);
		txtEcountry.setBounds(101, 191, 110, 20);
		equPanel.add(txtEcountry);
		
		txtEminStock = new JTextField();
		txtEminStock.setColumns(10);
		txtEminStock.setBounds(400, 36, 110, 20);
		equPanel.add(txtEminStock);
		
		txtEsupplierId = new JTextField();
		txtEsupplierId.setColumns(10);
		txtEsupplierId.setBounds(400, 67, 110, 20);
		equPanel.add(txtEsupplierId);
		
		txtEpType = new JTextField();
		txtEpType.setColumns(10);
		txtEpType.setBounds(400, 98, 110, 20);
		equPanel.add(txtEpType);
		
		txtEtype = new JTextField();
		txtEtype.setColumns(10);
		txtEtype.setBounds(400, 129, 110, 20);
		equPanel.add(txtEtype);
		
		txtEdescription = new JTextField();
		txtEdescription.setColumns(10);
		txtEdescription.setBounds(400, 160, 110, 20);
		equPanel.add(txtEdescription);
		
		JLabel lblEid = new JLabel("ID : ");
		lblEid.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEid.setBounds(-26, 39, 117, 14);
		equPanel.add(lblEid);
		
		lblEname = new JLabel("Name : ");
		lblEname.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEname.setBounds(-26, 70, 117, 14);
		equPanel.add(lblEname);
		
		lblEpurchasePrice = new JLabel("Purchase Price : ");
		lblEpurchasePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEpurchasePrice.setBounds(-26, 101, 117, 14);
		equPanel.add(lblEpurchasePrice);
		
		lblEsalesPrice = new JLabel("Sales Price : ");
		lblEsalesPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEsalesPrice.setBounds(-26, 132, 117, 14);
		equPanel.add(lblEsalesPrice);
		
		lblErentPrice = new JLabel("Rent Price : ");
		lblErentPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblErentPrice.setBounds(-26, 163, 117, 14);
		equPanel.add(lblErentPrice);
		
		lblEcountry = new JLabel("Country Origin : ");
		lblEcountry.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEcountry.setBounds(-26, 194, 117, 14);
		equPanel.add(lblEcountry);
		
		lblEminStock = new JLabel("Minimum Stock : ");
		lblEminStock.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEminStock.setBounds(273, 39, 117, 14);
		equPanel.add(lblEminStock);
		
		lblEsupplierId = new JLabel("Supplier ID : ");
		lblEsupplierId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEsupplierId.setBounds(273, 70, 117, 14);
		equPanel.add(lblEsupplierId);
		
		lblEproductType = new JLabel("Product Type : ");
		lblEproductType.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEproductType.setBounds(273, 101, 117, 14);
		equPanel.add(lblEproductType);
		
		lblEtype = new JLabel("Type : ");
		lblEtype.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEtype.setBounds(273, 132, 117, 14);
		equPanel.add(lblEtype);
		
		lblEdescription = new JLabel("Description : ");
		lblEdescription.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEdescription.setBounds(273, 163, 117, 14);
		equPanel.add(lblEdescription);
		
		btnEcreate = new JButton("Create");
		btnEcreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
				proCtrl.insertEquipment(Integer.parseInt(txtEid.getText()), txtEname.getText(), Double.parseDouble(txtEpurchasePrice.getText()), Double.parseDouble(txtEsalesPrice.getText()),
						Double.parseDouble(txtErentPrice.getText()), txtEcountry.getText(), Integer.parseInt(txtEminStock.getText()), Integer.parseInt(txtEsupplierId.getText()),
						txtEpType.getText(), txtEtype.getText(), txtEdescription.getText());
				JOptionPane.showMessageDialog(null,"Equipment product was created!");
				clearEquipmentTextFields();
				pTable.updateProductTable();
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Equipment!");
				}
			}
		});
		btnEcreate.setBounds(10, 222, 110, 23);
		equPanel.add(btnEcreate);
		
		btnEfind = new JButton("Find");
		btnEfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = proCtrl.getProductID(txtEid.getText());
				txtEname.setText(prod.getName());
				txtEpurchasePrice.setText(String.valueOf(prod.getPurchasePrice()));
				txtEsalesPrice.setText(String.valueOf(prod.getSalesPrice()));
				txtErentPrice.setText(String.valueOf(prod.getRentPrice()));
				txtEcountry.setText(prod.getCountry());
				txtEminStock.setText(String.valueOf(prod.getMinStock()));
				txtEsupplierId.setText(String.valueOf(prod.getSupplierID()));
				txtEpType.setText(prod.getpType());
				Equipment equi = proCtrl.getEquipmentID(txtEid.getText());
				txtEtype.setText(equi.getType());
				txtEdescription.setText(equi.getDescription());
			}
		});
		btnEfind.setBounds(130, 222, 110, 23);
		equPanel.add(btnEfind);
		
		btnEdelete = new JButton("Delete");
		btnEdelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				proCtrl.deleteProduct(txtEid.getText());
				proCtrl.deleteEquipment(txtEid.getText());
				clearEquipmentTextFields();
				pTable.updateProductTable();
			}
		});
		btnEdelete.setBounds(250, 222, 110, 23);
		equPanel.add(btnEdelete);
		
		btnEupdate = new JButton("Update");
		btnEupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = new Product();
				prod = proCtrl.getProductID(txtEid.getText());
				if(!txtEname.getText().equals("")) prod.setName(txtEname.getText());
				if(!txtEpurchasePrice.getText().equals("")) prod.setPurchasePrice(Double.parseDouble(txtEpurchasePrice.getText()));
				if(!txtEsalesPrice.getText().equals("")) prod.setSalesPrice(Double.parseDouble(txtEsalesPrice.getText()));
				if(!txtErentPrice.getText().equals("")) prod.setRentPrice(Double.parseDouble(txtErentPrice.getText()));
				if(!txtEcountry.getText().equals("")) prod.setCountry(txtEcountry.getText());
				if(!txtEminStock.getText().equals("")) prod.setMinStock(Integer.parseInt(txtEminStock.getText()));
				if(!txtEsupplierId.getText().equals("")) prod.setSupplierID(Integer.parseInt(txtEsupplierId.getText()));
				if(!txtEpType.getText().equals("")) prod.setpType(txtEpType.getText());
				Equipment equi = new Equipment();
				equi = proCtrl.getEquipmentID(txtEid.getText());
				if(!txtEtype.getText().equals("")) equi.setType(txtEtype.getText());
				if(!txtEdescription.getText().equals("")) equi.setDescription(txtEdescription.getText());
				proCtrl.updateEquipment(prod.getId(), prod.getName(), prod.getPurchasePrice(), prod.getSalesPrice(), prod.getRentPrice(), prod.getCountry(), prod.getMinStock(), prod.getSupplierID(), prod.getpType(), equi.getType(), equi.getDescription());
				pTable.updateProductTable();
			}
		});
		btnEupdate.setBounds(370, 222, 110, 23);
		equPanel.add(btnEupdate);
		
		JButton btnEquit = new JButton("Quit");
		btnEquit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnEquit.setBounds(519, 248, 90, 25);
		equPanel.add(btnEquit);
		
		btnEclear = new JButton("Clear Fields");
		btnEclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearEquipmentTextFields();
			}
		});
		btnEclear.setBounds(509, 190, 100, 25);
		equPanel.add(btnEclear);
		
		JPanel gunPanel = new JPanel();
		tabbedPane.addTab("Gun Replicas", null, gunPanel, null);
		gunPanel.setLayout(null);
		
		lblGmain = new JLabel("Create - Find - Delete - Update");
		lblGmain.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblGmain.setBounds(211, 11, 193, 14);
		gunPanel.add(lblGmain);
		
		txtGid = new JTextField();
		txtGid.setColumns(10);
		txtGid.setBounds(101, 36, 110, 20);
		gunPanel.add(txtGid);
		
		txtGname = new JTextField();
		txtGname.setColumns(10);
		txtGname.setBounds(101, 67, 110, 20);
		gunPanel.add(txtGname);
		
		txtGpurchasePrice = new JTextField();
		txtGpurchasePrice.setColumns(10);
		txtGpurchasePrice.setBounds(101, 98, 110, 20);
		gunPanel.add(txtGpurchasePrice);
		
		txtGsalesPrice = new JTextField();
		txtGsalesPrice.setColumns(10);
		txtGsalesPrice.setBounds(101, 129, 110, 20);
		gunPanel.add(txtGsalesPrice);
		
		txtGrentPrice = new JTextField();
		txtGrentPrice.setColumns(10);
		txtGrentPrice.setBounds(101, 160, 110, 20);
		gunPanel.add(txtGrentPrice);
		
		txtGcountry = new JTextField();
		txtGcountry.setColumns(10);
		txtGcountry.setBounds(101, 191, 110, 20);
		gunPanel.add(txtGcountry);
		
		txtGminStock = new JTextField();
		txtGminStock.setColumns(10);
		txtGminStock.setBounds(400, 36, 110, 20);
		gunPanel.add(txtGminStock);
		
		txtGsupplierId = new JTextField();
		txtGsupplierId.setColumns(10);
		txtGsupplierId.setBounds(400, 67, 110, 20);
		gunPanel.add(txtGsupplierId);
		
		txtGpType = new JTextField();
		txtGpType.setColumns(10);
		txtGpType.setBounds(400, 98, 110, 20);
		gunPanel.add(txtGpType);
		
		txtGfabric = new JTextField();
		txtGfabric.setColumns(10);
		txtGfabric.setBounds(400, 129, 110, 20);
		gunPanel.add(txtGfabric);
		
		txtGcaliber = new JTextField();
		txtGcaliber.setColumns(10);
		txtGcaliber.setBounds(400, 160, 110, 20);
		gunPanel.add(txtGcaliber);
		
		lblGid = new JLabel("ID : ");
		lblGid.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGid.setBounds(-26, 39, 117, 14);
		gunPanel.add(lblGid);
		
		lblGname = new JLabel("Name : ");
		lblGname.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGname.setBounds(-26, 70, 117, 14);
		gunPanel.add(lblGname);
		
		lblGpurchasePrice = new JLabel("Purchase Price : ");
		lblGpurchasePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGpurchasePrice.setBounds(-26, 101, 117, 14);
		gunPanel.add(lblGpurchasePrice);
		
		lblGsalesPrice = new JLabel("Sales Price : ");
		lblGsalesPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGsalesPrice.setBounds(-26, 132, 117, 14);
		gunPanel.add(lblGsalesPrice);
		
		lblGrentPrice = new JLabel("Rent Price : ");
		lblGrentPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGrentPrice.setBounds(-26, 163, 117, 14);
		gunPanel.add(lblGrentPrice);
		
		lblGcountry = new JLabel("Country Origin : ");
		lblGcountry.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGcountry.setBounds(-26, 194, 117, 14);
		gunPanel.add(lblGcountry);
		
		lblGminStock = new JLabel("Minimum Stock : ");
		lblGminStock.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGminStock.setBounds(273, 39, 117, 14);
		gunPanel.add(lblGminStock);
		
		lblGsupplierId = new JLabel("Supplier ID : ");
		lblGsupplierId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGsupplierId.setBounds(273, 70, 117, 14);
		gunPanel.add(lblGsupplierId);
		
		lblGpType = new JLabel("Product Type : ");
		lblGpType.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGpType.setBounds(273, 101, 117, 14);
		gunPanel.add(lblGpType);
		
		lblGfabric = new JLabel("Fabric : ");
		lblGfabric.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGfabric.setBounds(273, 132, 117, 14);
		gunPanel.add(lblGfabric);
		
		lblGcaliber = new JLabel("Caliber : ");
		lblGcaliber.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGcaliber.setBounds(273, 163, 117, 14);
		gunPanel.add(lblGcaliber);
		
		btnGcreate = new JButton("Create");
		btnGcreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
				proCtrl.insertGunReplicas(Integer.parseInt(txtGid.getText()), txtGname.getText(), Double.parseDouble(txtGpurchasePrice.getText()), Double.parseDouble(txtGsalesPrice.getText()),
						Double.parseDouble(txtGrentPrice.getText()), txtGcountry.getText(), Integer.parseInt(txtGminStock.getText()), Integer.parseInt(txtGsupplierId.getText()),
						txtGpType.getText(), txtGfabric.getText(), txtGcaliber.getText());
				JOptionPane.showMessageDialog(null,"Gun Replica product was created!");
				clearGunReplicasTextFields();
				pTable.updateProductTable();
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Gun Replica!");
				}
			}
		});
		btnGcreate.setBounds(10, 222, 110, 23);
		gunPanel.add(btnGcreate);
		
		btnGfind = new JButton("Find");
		btnGfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = proCtrl.getProductID(txtGid.getText());
				txtGname.setText(prod.getName());
				txtGpurchasePrice.setText(String.valueOf(prod.getPurchasePrice()));
				txtGsalesPrice.setText(String.valueOf(prod.getSalesPrice()));
				txtGrentPrice.setText(String.valueOf(prod.getRentPrice()));
				txtGcountry.setText(prod.getCountry());
				txtGminStock.setText(String.valueOf(prod.getMinStock()));
				txtGsupplierId.setText(String.valueOf(prod.getSupplierID()));
				txtGpType.setText(prod.getpType());
				GunReplicas gunr = proCtrl.getGunReplicasID(txtGid.getText());
				txtGfabric.setText(gunr.getFabric());
				txtGcaliber.setText(gunr.getCaliber());
			}
		});
		btnGfind.setBounds(130, 222, 110, 23);
		gunPanel.add(btnGfind);
		
		btnGdelete = new JButton("Delete");
		btnGdelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				proCtrl.deleteProduct(txtGid.getText());
				proCtrl.deleteGunReplicas(txtGid.getText());
				clearGunReplicasTextFields();
				pTable.updateProductTable();
			}
		});
		btnGdelete.setBounds(250, 222, 110, 23);
		gunPanel.add(btnGdelete);
		
		btnGupdate = new JButton("Update");
		btnGupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Product prod = new Product();
				prod = proCtrl.getProductID(txtGid.getText());
				if(!txtGname.getText().equals("")) prod.setName(txtGname.getText());
				if(!txtGpurchasePrice.getText().equals("")) prod.setPurchasePrice(Double.parseDouble(txtGpurchasePrice.getText()));
				if(!txtGsalesPrice.getText().equals("")) prod.setSalesPrice(Double.parseDouble(txtGsalesPrice.getText()));
				if(!txtGrentPrice.getText().equals("")) prod.setRentPrice(Double.parseDouble(txtGrentPrice.getText()));
				if(!txtGcountry.getText().equals("")) prod.setCountry(txtGcountry.getText());
				if(!txtGminStock.getText().equals("")) prod.setMinStock(Integer.parseInt(txtGminStock.getText()));
				if(!txtGsupplierId.getText().equals("")) prod.setSupplierID(Integer.parseInt(txtGsupplierId.getText()));
				if(!txtGpType.getText().equals("")) prod.setpType(txtGpType.getText());
				GunReplicas gunr = new GunReplicas();
				gunr = proCtrl.getGunReplicasID(txtGid.getText());
				if(!txtGfabric.getText().equals("")) gunr.setFabric(txtGfabric.getText());
				if(!txtGcaliber.getText().equals("")) gunr.setCaliber(txtGcaliber.getText());
				proCtrl.updateGunReplicas(prod.getId(), prod.getName(), prod.getPurchasePrice(), prod.getSalesPrice(), prod.getRentPrice(), prod.getCountry(), prod.getMinStock(), prod.getSupplierID(), prod.getpType(), gunr.getFabric(), gunr.getCaliber());
				pTable.updateProductTable();
			}
		});
		btnGupdate.setBounds(370, 222, 110, 23);
		gunPanel.add(btnGupdate);
		
		JButton btnGsquit = new JButton("Quit");
		btnGsquit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnGsquit.setBounds(519, 248, 90, 25);
		gunPanel.add(btnGsquit);
		
		btnGclear = new JButton("Clear Fields");
		btnGclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearGunReplicasTextFields();
			}
		});
		btnGclear.setBounds(509, 190, 100, 25);
		gunPanel.add(btnGclear);
		
		JButton btnSquit = new JButton("Quit");
		btnSquit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(EXIT_ON_CLOSE);
			}
		});
		btnSquit.setBounds(519, 248, 90, 25);
		supPanel.add(btnSquit);
		
		JLabel lblSmain = new JLabel("Create - Find - Delete - Update");
		lblSmain.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblSmain.setBounds(54, 11, 193, 14);
		supPanel.add(lblSmain);
		
		txtSname = new JTextField();
		txtSname.setBounds(137, 36, 110, 20);
		supPanel.add(txtSname);
		txtSname.setColumns(10);
		
		txtSaddress = new JTextField();
		txtSaddress.setColumns(10);
		txtSaddress.setBounds(137, 67, 110, 20);
		supPanel.add(txtSaddress);
		
		txtScountry = new JTextField();
		txtScountry.setColumns(10);
		txtScountry.setBounds(137, 98, 110, 20);
		supPanel.add(txtScountry);
		
		txtSphoneNo = new JTextField();
		txtSphoneNo.setColumns(10);
		txtSphoneNo.setBounds(137, 129, 110, 20);
		supPanel.add(txtSphoneNo);
		
		txtSemail = new JTextField();
		txtSemail.setColumns(10);
		txtSemail.setBounds(137, 160, 110, 20);
		supPanel.add(txtSemail);
		
		JLabel lblSname = new JLabel("Name : ");
		lblSname.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSname.setBounds(10, 39, 117, 14);
		supPanel.add(lblSname);
		
		JLabel lblSaddress = new JLabel("Address : ");
		lblSaddress.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSaddress.setBounds(10, 70, 117, 14);
		supPanel.add(lblSaddress);
		
		JLabel lblScountry = new JLabel("Country : ");
		lblScountry.setHorizontalAlignment(SwingConstants.RIGHT);
		lblScountry.setBounds(10, 101, 117, 14);
		supPanel.add(lblScountry);
		
		JLabel lblSphoneNo = new JLabel("Phone Number : ");
		lblSphoneNo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSphoneNo.setBounds(10, 132, 117, 14);
		supPanel.add(lblSphoneNo);
		
		JLabel lblSemail = new JLabel("Email : ");
		lblSemail.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSemail.setBounds(10, 163, 117, 14);
		supPanel.add(lblSemail);
		
		JButton btnScreate = new JButton("Create");
		btnScreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
				supCtrl.insertSupplier(txtSname.getText(), txtSaddress.getText(), txtScountry.getText(), Integer.parseInt(txtSphoneNo.getText()), txtSemail.getText());
				JOptionPane.showMessageDialog(null,"Supplier was created!");
				clearSupplierTextFields();
				updateSupplierTable();
				pTable.updateProductTable();
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in creating Supplier!");
				}
			}
		});
		btnScreate.setBounds(10, 191, 110, 23);
		supPanel.add(btnScreate);
		
		JButton btnSupdate = new JButton("Update");
		btnSupdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Supplier supp = new Supplier();
				supp = supCtrl.getSupplierName(txtSname.getText());
				if(!txtSaddress.getText().equals("")) supp.setAddress(txtSaddress.getText());
				if(!txtScountry.getText().equals("")) supp.setCountry(txtScountry.getText());
				if(!txtSphoneNo.getText().equals("")) supp.setPhoneno(Integer.parseInt(txtSphoneNo.getText()));
				if(!txtSemail.getText().equals("")) supp.setEmail(txtSemail.getText());
				supCtrl.updateSupplier(supp.getName(), supp.getAddress(), supp.getCountry(), supp.getPhoneno(), supp.getEmail());
				updateSupplierTable();
				pTable.updateProductTable();
			}
		});
		btnSupdate.setBounds(137, 225, 110, 23);
		supPanel.add(btnSupdate);
		
		JButton btnSdelete = new JButton("Delete");
		btnSdelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
				{
					supCtrl.deleteSupplier(txtSphoneNo.getText());
					JOptionPane.showMessageDialog(null,"Supplier was deleted!");
					clearSupplierTextFields();
					updateSupplierTable();
					pTable.updateProductTable();
				}
				catch(Exception r)
				{
					JOptionPane.showMessageDialog(null,"Error in deleting Supplier!");
				}
			}
		});
		btnSdelete.setBounds(10, 225, 110, 23);
		supPanel.add(btnSdelete);
		
		JButton btnSfind = new JButton("Find");
		btnSfind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					Supplier supp = supCtrl.getSupplierName(txtSname.getText());
					txtSname.setText(supp.getName());
					txtSaddress.setText(supp.getAddress());
					txtScountry.setText(supp.getCountry());
					txtSphoneNo.setText(String.valueOf(supp.getPhoneno()));
					txtSemail.setText(supp.getEmail());
				}
				catch(Exception r)
				{
					
				}
			}
		});
		btnSfind.setBounds(137, 191, 110, 23);
		supPanel.add(btnSfind);
		
		sList = new SupplierTableList();
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(257, 11, 352, 226);
		supPanel.add(scrollPane);
		
		table = new JTable();
		
		table.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][]{},
	            new String []
	            {
	            		"Name", "Address", "Country", "PhoneNo", "Email"
	            })
				{
	            Class[] types = new Class []
	            {
	                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
	            };

	            public Class getColumnClass(int columnIndex)
	            {
	                return types [columnIndex];
	            }
				});
		scrollPane.setViewportView(table);
		
		btnSclear = new JButton("Clear Fields");
		btnSclear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearSupplierTextFields();
			}
		});
		btnSclear.setBounds(257, 249, 100, 25);
		supPanel.add(btnSclear);
		
		btnListProducts = new JButton("List Products");
		btnListProducts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pTable.setVisible(true);
				pTable.updateProductTable();
			}
		});
		btnListProducts.setBounds(367, 249, 110, 25);
		supPanel.add(btnListProducts);
	}
	
	public void updateSupplierTable()
	{
        sList.setS(supCtrl.getAllSuppliers());
        table.setModel(sList);
        table.setAutoCreateRowSorter(true);
	}
	
	private void clearSupplierTextFields()
	{
		txtSname.setText("");
		txtSaddress.setText("");
		txtScountry.setText("");
		txtSphoneNo.setText("");
		txtSemail.setText("");
	}
	
	private void clearClothingTextFields()
	{
		txtCid.setText("");
		txtCname.setText("");
		txtCpurchasePrice.setText("");
		txtCsalesPrice.setText("");
		txtCrentPrice.setText("");
		txtCcountry.setText("");
		txtCminStock.setText("");
		txtCsupplierId.setText("");
		txtCpType.setText("");
		txtCsize.setText("");
		txtCcolor.setText("");
	}
	
	private void clearEquipmentTextFields()
	{
		txtEid.setText("");
		txtEname.setText("");
		txtEpurchasePrice.setText("");
		txtEsalesPrice.setText("");
		txtErentPrice.setText("");
		txtEcountry.setText("");
		txtEminStock.setText("");
		txtEsupplierId.setText("");
		txtEpType.setText("");
		txtEtype.setText("");
		txtEdescription.setText("");
	}
	
	private void clearGunReplicasTextFields()
	{
		txtGid.setText("");
		txtGname.setText("");
		txtGpurchasePrice.setText("");
		txtGsalesPrice.setText("");
		txtGrentPrice.setText("");
		txtGcountry.setText("");
		txtGminStock.setText("");
		txtGsupplierId.setText("");
		txtGpType.setText("");
		txtGfabric.setText("");
		txtGcaliber.setText("");
	}
	
	public static boolean isNumeric(String _str)
	{
		try
		{
			long l = Long.parseLong(_str);
		}
		catch(NumberFormatException nfe)
		{
			return false;
		}
		return true;
	}
}
